package mylab.io.store.socket.property;

import static io.netty.util.NetUtil.LOOPBACK_IF;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.net.NetworkInterface;

import lombok.Getter;
import lombok.Setter;
import mylab.io.base.model.impl.SocketEndpoint;
import mylab.io.base.property.BaseDasProperties;
import mylab.io.base.property.DasProperties;

@Getter
@Setter
public class UdpMulticastProperties extends BaseDasProperties {
    static final String PROPERTIES_PREFIX = DasProperties.PROPERTIES_PREFIX + ".udp.multicast";

    /**
     * 224.0.2.0～238.255.255.255 为用户可用的组播地址（临时组地址），全网范围内有效；
     */
    SocketEndpoint group;
    SocketEndpoint local;

    int ttl = 255;
    boolean loopbackModeDisabled = true;
    boolean reuseAddress = true;
    String networkInterfaceName;

    @JsonIgnore
    public NetworkInterface getNetworkInterface() {
        try {
            if (networkInterfaceName != null) {
                return NetworkInterface.getByName(networkInterfaceName);
            } else {
                return LOOPBACK_IF;
            }
        } catch (Exception e) {
            return LOOPBACK_IF;
        }
    }

}
